//
//  BBLink.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/7.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit

class BBLink: NSObject {
    @objc var linkType:NSNumber?
    @objc var linkId:String?
    @objc var linkImages:String?
    @objc var descriptionOC:String?
    @objc var mileage:NSNumber?
    @objc var usedTime:NSNumber?
    @objc var times:NSNumber?
    @objc var zan:NSNumber?
    @objc var userLink:BBUser?
    @objc var createTime:NSNumber?
    @objc var updateTime:NSNumber?
    var dictTem:[String:AnyObject]?
    
    init(dict:[String:AnyObject]){
        super.init()
        dictTem = dict
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        if key == "description"{
            descriptionOC = (dictTem![key] as! String)
        }
        if key == "user"{
            userLink = BBUser.init(dict: dictTem![key] as! [String:AnyObject])
        }
        print(key)
    }
    
    class func loadLinks(pageNum:Int,finsihed:@escaping (_ models:[BBLink]?,_ error:Error?)->()){
        let path = "link/list.do"
        let params = ["token":BBUser.loadUser()?.token,"userId":BBUser.loadUser()?.userId,"pageNum":String(pageNum)]
        AFNetWorkManager.shareNetworkTools().post(path, parameters: params, progress: { (_) in
            
        }, success: { (_, json) in
            let jsonDic = json as! [String:AnyObject]
            let dictStatus = jsonDic["status"]as!NSNumber
            if(dictStatus == 0){
                let data = jsonDic["data"]
                let models = dict2Model(list: data!["list"] as! [[String:AnyObject]])
                finsihed(models,nil)
            }else{
                finsihed(nil,nil)
            }
        }) { (_, error) in
            finsihed(nil,error)
        }
    }
    

    class func dict2Model(list:[[String:AnyObject]])->[BBLink]{
        var models = [BBLink]()
        for dict in list{
            models.append(BBLink(dict: dict))
        }
        return models
    }
}
